package com.LostAndFound.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import com.LostAndFound.domain.Article;
import com.LostAndFound.domain.FoundArticle;
import com.LostAndFound.domain.SystemInfo;
import com.LostAndFound.utils.DataSourceUtils;

/*
 * 此MessageDao.java已经弃用
 */

public class MessageDao {

	QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
	String sql = null;
	
	public List<SystemInfo> getSystemInfoList() throws SQLException {
		sql = "select * from info order by datetime desc";
		return runner.query(sql, new BeanListHandler<SystemInfo>(SystemInfo.class));
	}

	public List<Article> getArticleOrderList(String uid) throws SQLException {
		sql = "select a.*,c.cname from article a,category c "
				+ " where a.cid = c.cid and a.foundflag = 2 and uid = ?";
		return runner.query(sql, new BeanListHandler<Article>(Article.class), uid);
	}

	public Map<String, Object> showArticleOrderDetail(String aid) throws SQLException {
		sql = "select a.*,c.cname from article a,category c where a.cid = c.cid and aid = ?";
		return runner.query(sql, new MapHandler(),aid);
	}

	public void cancelOrderArticle(String uid) throws SQLException {
		sql = "update article set foundflag = 1 where aid = ?";
		runner.update(sql, uid);
	}

	//插入 foundArticle 表
	public void handleAddFoundArticle(FoundArticle foundArticle) throws SQLException {
		sql = "insert into foundarticle(aid,foundtime,foundinfo) values(?,?,?)";
		Object[] params = {foundArticle.getArticle().getAid(),foundArticle.getFoundTime(),foundArticle.getFoundInfo()};
		runner.update(sql, params);
	}
	
	//更新article表的 foundFlag 字段
	public void handleUpdateFoundFlag(FoundArticle foundArticle) throws SQLException {
		sql = "update article set foundflag = 0 where aid = ?";
		runner.update(sql, foundArticle.getArticle().getAid());
	}

	public SystemInfo showSystemInfoDetailByInfoId(String infoid) throws SQLException {
		sql = "select * from info where infoid = ?";
		return runner.query(sql, new BeanHandler<SystemInfo>(SystemInfo.class), infoid);
	}

}
